package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.log;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import java.net.URLEncoder;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.LogExcelEn;
import com.xiaoxu.intranetweb.util.SzResult;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/SysLog")
public class SysLogController {
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private UserInfoDao userInfoDao;

    /**
     * 查询日志
     *
     * @param page:      页码
     * @param pageSize:  每页条数
     * @param startTime: 开始时间
     * @param endTime:   结束时间
     * @param value:     模糊查询
     * @return SzResult
     */
    @GetMapping("/getLog")
    public SzResult getLog(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "value", required = false) String value,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        {
            if (page == null || page.equals(""))
                page = "1";
            if (pageSize == null || pageSize.equals(""))
                pageSize = "10";
            return sysLogBu.logQuery(page, pageSize, startTime, endTime, value, plantId);
        }
    }



    /**
     * 日志导出excel
     *
     * @param jsonParam
     * @param response
     * @param request
     * @throws Exception
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");      String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token != null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
        String filename = jsonParam.getString("filename");
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
        boolean isHeader = jsonParam.getBoolean("isHeader");
        String mode = jsonParam.getString("mode");
        Boolean original = jsonParam.getBoolean("original");
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        String logType = null;
        String logName = null;
        String startTime = null;
        String endTime = null;
        if (form.get("logType") != null && !form.get("logType").equals("null")) {
            logType = form.get("logType");
        }
        if (form.get("logName") != null && !form.get("logName").equals("null")) {
            logName = form.get("logName");
        }
        if (form.get("startTime") != null && !form.get("startTime").equals("null")) {
            startTime = form.get("startTime");
        }
        if (form.get("endTime") != null && !form.get("endTime").equals("null")) {
            endTime = form.get("endTime");
        }
        Future<List<LogExcelEn>> listFuture = sysLogBu.exportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, plantId, logType,logName,startTime,endTime);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<LogExcelEn> logExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), LogExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("日志")
                .doWrite(logExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
